#include "cv.h"
#include "highgui.h"
#include <iostream>

#define CAMERA -1
//see below for an explanation on this constant

using namespace std;

/*
Displays streaming video.

example in terminal:

./program2

OR (if it worked, which it doesn't)

./program2 video.avi


Note: There is a flaw in OpenCV so the second command won't work

CAMERA CONSTANT:
The camera constant determines which camera OpenCV will use for cvCreateCameraCapture().

0 = any camera (but usually chooses first)
1 = USB
-1 = open a window to choose which camera to use (doesn't work-- just chooses first one)
300 = choose firewire or IEEE1394 (doesn't work)

*/

int main( int argc, char** argv ) {

	cvNamedWindow( "Stream Video", CV_WINDOW_AUTOSIZE );		//create window with default size
	CvCapture* capture;						//create a capture object
	if(argc == 1){							//error-checking
		capture = cvCreateCameraCapture( CAMERA );		//pick any camera if multiple cameras
	}
	else{
		capture = cvCreateFileCapture(argv[1]);			//pick video file specified
	}

	IplImage* frame;						//create image object
	while(1) {
		frame = cvQueryFrame( capture );			//break up the stream into images
		
		if (!frame){						//error-checking
			cout <<"Error :(\n" ;
			break;
		}
		
		cvShowImage("Stream Video", frame);
		char c = cvWaitKey(20);					//20ms between each image/frame
		if (c == 27) break;					//if user presses 'esc', loop breaks (program ends)
	}
	cvReleaseCapture( &capture );
	cvDestroyWindow( "Stream Video" );
}
